package cn.iocoder.an.module.business.controller.admin.reportview;

import cn.hutool.core.lang.tree.Tree;
import cn.iocoder.an.framework.common.enums.CommonStatusEnum;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.an.module.business.dal.mysql.reportview.ReportViewMapper;
import cn.iocoder.an.module.business.utils.WeComMessage;
import cn.iocoder.an.module.business.utils.WeComRobotUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.reportview.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.reportview.ReportViewDO;
import cn.iocoder.an.module.business.service.reportview.ReportViewService;

@Tag(name = "管理后台 - 报表汇总")
@RestController
@RequestMapping("/business/report-view")
@Validated
@TenantIgnore
public class ReportViewController {

    @Resource
    private ReportViewService reportViewService;

    @Resource
    private ReportViewMapper reportViewMapper;

    @Resource
    private WeComRobotUtil weComRobotUtil;


    @Value("${wecom.robot.default-webhook}") // ✅ 正确使用 Spring 的 @Value
    private String defaultWebhook;

    // 示例：新增一个推送消息的方法
    @GetMapping("/sendMessage")
    public CommonResult<String> testPush(@RequestParam("id") Long id) {
        var message = WeComMessage.text("测试消息", "17678970797");
        boolean success = weComRobotUtil.send(defaultWebhook, message);
        return null;
    }



    @PostMapping("/create")
    @Operation(summary = "创建报表汇总")
    @PreAuthorize("@ss.hasPermission('business:report-view:create')")
    public CommonResult<Long> createReportView(@Valid @RequestBody ReportViewDO reportView) {
        return success(reportViewService.createReportView(reportView));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报表汇总")
    @PreAuthorize("@ss.hasPermission('business:report-view:update')")
    public CommonResult<Boolean> updateReportView(@Valid @RequestBody ReportViewDO reportView) {
        reportViewService.updateReportView(reportView);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报表汇总")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:report-view:delete')")
    public CommonResult<Boolean> deleteReportView(@RequestParam("id") Long id) {
        reportViewService.deleteReportView(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报表汇总")
                @PreAuthorize("@ss.hasPermission('business:report-view:delete')")
    public CommonResult<Boolean> deleteReportViewList(@RequestParam("ids") List<Long> ids) {
        reportViewService.deleteReportViewListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报表汇总")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:report-view:query')")
    public CommonResult<ReportViewDO> getReportView(@RequestParam("id") Long id) {
        ReportViewDO reportView = reportViewService.getReportView(id);
        return success(reportView);
    }

    @GetMapping("/page")
    @Operation(summary = "获得报表汇总分页")
    @PreAuthorize("@ss.hasPermission('business:report-view:query')")
    public CommonResult<PageResult<ReportViewDO>> getReportViewPage(@Valid ReportViewPageReqVO pageReqVO) {
        PageResult<ReportViewDO> pageResult = reportViewService.getReportViewPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/configPage")
    @Operation(summary = "获得报表汇总分页")
    @PreAuthorize("@ss.hasPermission('business:report-view:query')")
    public CommonResult<PageResult<ReportViewDO>> getReportViewConfigPage(@Valid ReportViewPageReqVO pageReqVO) {
        PageResult<ReportViewDO> pageResult = reportViewService.getReportViewPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/list")
    public CommonResult<List<ReportViewDO>> getReportViewList(@Valid ReportViewPageReqVO pageReqVO) {
        LambdaQueryWrapperX<ReportViewDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(ReportViewDO::getParentId, 100L);
        List<ReportViewDO> reportViewDOS = reportViewMapper.selectList(queryWrapper);
        return success(reportViewDOS);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报表汇总 Excel")
    @PreAuthorize("@ss.hasPermission('business:report-view:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportViewExcel(@Valid ReportViewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportViewDO> list = reportViewService.getReportViewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报表汇总.xls", "数据", ReportViewDO.class,
                        BeanUtils.toBean(list, ReportViewDO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取部门精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项")
    public CommonResult<List<ReportViewDO>> getSimpleDeptList() {
        return success(reportViewService.getReportList());
    }

    /**
     * 获取树形菜单
     * @param reqVO 名称
     * @return 包含树形菜单的响应结果
     */
    @GetMapping(value = "/tree")
    public CommonResult<List<Tree<Long>>> getTree(ReportViewPageReqVO reqVO) {
        return success(reportViewService.selectTree(reqVO));
    }


}